package com.example.test_witns.adapter.message;

import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_witns.R;
import com.example.test_witns.pojo.Message;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.MessageViewHolder> {

    private List<Message> messages;

    public MessagesAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_send, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        Message message = messages.get(position);
        holder.itemView.setActivated(false);
        holder.receiverTextView.setVisibility(View.INVISIBLE);
        holder.recv_messageTextView.setVisibility(View.INVISIBLE);
        holder.receiver_avatar.setVisibility(View.INVISIBLE);
        holder.sender_avatar.setVisibility(View.INVISIBLE);
        holder.messageTextView.setVisibility(View.INVISIBLE);
        holder.senderTextView.setVisibility(View.INVISIBLE);
        holder.timestampTextView.setVisibility(View.INVISIBLE);
        if (message.isSend()){
            holder.senderTextView.setText(message.getSenderNickname());
            holder.messageTextView.setText(message.getText());
            holder.timestampTextView.setText(message.getTimestamp());
            if (message.getAvatarBytes() != null) holder.sender_avatar.setImageBitmap(BitmapFactory.decodeByteArray(message.getAvatarBytes(), 0, message.getAvatarBytes().length));
            holder.sender_avatar.setVisibility(View.VISIBLE);
            holder.messageTextView.setVisibility(View.VISIBLE);
            holder.senderTextView.setVisibility(View.VISIBLE);
            holder.timestampTextView.setVisibility(View.VISIBLE);
        }else {
            holder.receiverTextView.setText(message.getSenderNickname());
            holder.recv_messageTextView.setText(message.getText());
            holder.timestampTextView.setText(message.getTimestamp());
            if (message.getAvatarBytes() != null) holder.receiver_avatar.setImageBitmap(BitmapFactory.decodeByteArray(message.getAvatarBytes(), 0, message.getAvatarBytes().length));
            holder.timestampTextView.setVisibility(View.VISIBLE);
            holder.receiverTextView.setVisibility(View.VISIBLE);
            holder.recv_messageTextView.setVisibility(View.VISIBLE);
            holder.receiver_avatar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public static class MessageViewHolder extends RecyclerView.ViewHolder {
        public TextView senderTextView;
        public TextView messageTextView;
        public TextView timestampTextView;

        public TextView receiverTextView;
        public TextView recv_messageTextView;
        public CircleImageView receiver_avatar;
        public CircleImageView sender_avatar;

        public MessageViewHolder(View itemView) {
            super(itemView);
            senderTextView = itemView.findViewById(R.id.senderTextView);
            messageTextView = itemView.findViewById(R.id.messageTextView);
            timestampTextView = itemView.findViewById(R.id.timestampTextView);
            receiverTextView = itemView.findViewById(R.id.receiver);
            recv_messageTextView = itemView.findViewById(R.id.receive_messageTextView);
            receiver_avatar = itemView.findViewById(R.id.receiver_imageButton);
            sender_avatar = itemView.findViewById(R.id.sender_avatar);
        }
    }

    public void addMessage(Message message) {
        messages.add(message);
        notifyItemInserted(messages.size() - 1);
    }

    public void addMessageReverse(Message message) {
        messages.add(0, message);
        notifyDataSetChanged();
    }
}